package cn.itcast.bos.web.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionChainResult;
import com.opensymphony.xwork2.ActionContext;
import com.sun.xml.bind.v2.model.core.ID;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {

	// 引入日志
	private final static Logger LOGGER = Logger.getLogger(WayBillAction.class);

	@Autowired
	private WayBillService wayBillService;

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		// 因为客户端需要的json格式是{success:true,msg:"xxx"}
		Map<String, Object> result = new HashMap<>();

		try {
			//为了防止运单中没有order对象,需要在保存之前将没有order对象的该字段置为null
			if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder()
					.getId() == 0)) {
				model.setOrder(null);
			}
			wayBillService.save(model);
			result.put("success", true);
			result.put("msg", "快速保存运单成功了");
			LOGGER.info("快速保存运单成功了,运单号是:" + model.getWayBillNum());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "快速保存运单失败了,运单号是:" + model.getWayBillNum());
			LOGGER.error("快速保存运单失败, 运单号是:" + model.getWayBillNum(), e);
		}
		// 压入值栈
		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	// 分页查询
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 无条件查询
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
		Page<WayBill> pageData = wayBillService.findPageData(model,pageable);
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}

	// findByWayBillNum,查询运单号 回显到界面
	@Action(value = "waybill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {
		//调用service
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		//向后台发送数据
		Map<String, Object> result = new HashMap<>();
		if (wayBill == null) {
			result.put("success", false);
		}else{
			result.put("success", true);
			result.put("wayBillData", wayBill);
		}
		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
